package com.java.main;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SimpleFetch {

    public static void main(String[] args) {

        //声明将要爬取的网站地址
        String path = "http://www.taohaoma.com/mobile/number";
        URL url;
        URLConnection con;
        InputStream is = null;
        InputStreamReader ir;
        BufferedReader br = null;
        try {
            //获取网络地址
            url = new URL(path);
            //获取网络连接
            con = url.openConnection();
            //获取字符输入流
            is = con.getInputStream();
            //获取字节输入流
            ir = new InputStreamReader(is);
            //获取字节缓冲流
            br = new BufferedReader(ir);
            //获取文件字节输出流
            FileWriter fw = new FileWriter("F:/fetch.txt");
            //获取缓冲字符输出流
            PrintWriter pw = new PrintWriter(fw);
            //声明一个正则规则
            String rgdex = "1[13456789]\\d{9}";
            //创建一个模式
            Pattern p = Pattern.compile(rgdex);
            String str,str1;
            while ((str = br.readLine()) !=null){
                Matcher m = p.matcher(str);
                while (m.find()){
                    pw.println(m.group());
                }
            }
            pw.close();
            System.out.println("爬取成功");

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }

}
